package com.kcl.dfss.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class AjustView extends ImageView {

	private Bitmap mBackgroundBitmap = null;
	private Paint mPaint = null;
	private float mXstart = 0.0f;
	private float mYstart = 0.0f;
	private int mWidth = 0;
	private int mHeight = 0;
	private int mPointIndex = 0;
	private float startX = 0;
	private float startY = 0;
	private float lastX = 0;
	private float lastY = 0;
	private boolean mMoveVertical = false;
	private boolean mMoveHorizontal = false;
	private float mTans = 0.577f;// tan30
	private boolean minit = true;
	private String TAG = "AjustView";

	private int mHalfWidth = 150;
	private int mHalfHeight = 150;

	public AjustView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		setBackgroundColor(Color.TRANSPARENT);
		init();
	}

	public AjustView(Context context, AttributeSet attr) {
		super(context, attr);
		init();
	}

	public void setImageBitmap(Bitmap bitmap) {
		Log.d(TAG, "set bitmap");
		mBackgroundBitmap = bitmap;
		invalidate();
	}

	private void init() {
		mPaint = new Paint();

	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (minit) {
			mWidth = getMeasuredWidthAndState();
			mHeight = getMeasuredHeightAndState();
			mXstart = mWidth / 2.0f;
			mYstart = mHeight / 2.0f;
			minit = false;
		}

		if (mBackgroundBitmap != null) {
			canvas.drawBitmap(mBackgroundBitmap, new Matrix(), mPaint);
		}

		mPaint.setStyle(Style.STROKE);// 设置非填充
		mPaint.setStrokeWidth(5);// 笔宽5像素
//		mPaint.setColor();// 设置为红笔
		mPaint.setAntiAlias(true);// 锯齿不显示

		if (mYstart != (mHeight / 2.0f - mHalfHeight)
				&& mYstart != (mHeight / 2.0f + mHalfHeight)) {
			mPaint.setColor(Color.parseColor("#4b8796"));
		} else {
			mPaint.setColor(Color.RED);
		}
		canvas.drawLine(0, mYstart, mWidth, mYstart, mPaint);

		if (mXstart != (mWidth / 2.0f - mHalfWidth)
				&& mXstart != (mWidth / 2.0f + mHalfWidth)) {
			mPaint.setColor(Color.DKGRAY);
		} else {
			mPaint.setColor(Color.RED);
		}

		canvas.drawLine(mXstart, 0, mXstart, mHeight, mPaint);
	}

	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			mPointIndex = 0;
			startX = event.getX();
			startY = event.getY();
			lastX = startX;
			lastY = startY;
			mMoveVertical = false;
			mMoveHorizontal = false;
			break;
		case MotionEvent.ACTION_MOVE:
			mPointIndex++;
			if (mPointIndex == 2) {
				float Ytan = (startX - event.getX()) / (startY - event.getY());
				Log.d(TAG, "Ytan is " + Ytan);
				if ((Ytan > -mTans && Ytan < 0) || (Ytan < mTans && Ytan > 0)) {
					mMoveVertical = true;
				} else {
					float Xtan = 1 / Ytan;
					if ((Xtan > -mTans && Xtan < 0)
							|| (Xtan < mTans && Xtan > 0)) {
						mMoveHorizontal = true;
					}
				}
			}
			if (mMoveHorizontal) {
				mXstart += (event.getX() - lastX);
				if (mXstart > (mWidth / 2.0f + mHalfWidth)) {
					mXstart = mWidth / 2.0f + mHalfWidth;
				} else if (mXstart < (mWidth / 2.0f - mHalfWidth)) {
					mXstart = (mWidth / 2.0f - mHalfWidth);
				}
				invalidate();
			}
			if (mMoveVertical) {
				mYstart += (event.getY() - lastY);
				if (mYstart > (mHeight / 2.0f + mHalfHeight)) {
					mYstart = mHeight / 2.0f + mHalfHeight;
				} else if (mYstart < (mHeight / 2.0f - mHalfHeight)) {
					mYstart = (mHeight / 2.0f - mHalfHeight);
				}
				invalidate();
			}
			lastX = event.getX();
			lastY = event.getY();
			break;
		case MotionEvent.ACTION_UP:
			Log.d(TAG, "mXstart and mYstart are " + mXstart + " " + mYstart);
			Log.d(TAG, "mWidth and mHeight are " + mWidth + "  " + mHeight);
			break;
		default:
			break;
		}
		return true;
	}

	public int[] getAjustResult() {
		int[] array = new int[2];
		array[0] = (int) (mXstart / mWidth * 640);
		array[1] = (int) (mYstart / mHeight * 480);
		return array;
	}
}